{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c168d69e-4ec2-4a56-b203-8da87e406e7b",
   "metadata": {},
   "source": [
    "### ⚙️ SQL Queries using `Python` 🐍 \n",
    "#### Loading the results in a Pandas `DataFrame` 🐼 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a43dfc56-ad0b-43c9-8002-05f311f73a5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install psycopg2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7561660d-9f24-4395-9444-16e20ad8c571",
   "metadata": {},
   "outputs": [],
   "source": [
    "import psycopg2\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fb3f2af1-c019-4c33-b01e-ef14e5feb696",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setting up connection; database is publicly available\n",
    "conn = psycopg2.connect(host='hh-pgsql-public.ebi.ac.uk', \n",
    "                        dbname='pfmegrnargs',\n",
    "                        user='reader', \n",
    "                        password='NWDMCE5xdipIjRrp')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "66c0be6a-032a-49f9-994d-cdb1178451c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# SQL Query\n",
    "query = \"SELECT * FROM rnc_database\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8ab80cc8-8c60-4921-a653-f8294ae6ef2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Executing the query\n",
    "cursor = conn.cursor()\n",
    "cursor.execute(query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "aea4b0cf-c74c-468e-9b5a-ff6584367281",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Loading the results in a pandas DataFrame\n",
    "df = pd.DataFrame(cursor.fetchall(), \n",
    "                  columns=[desc[0] for desc in cursor.description])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7f897634-a95a-4e83-ace1-61abf3da922c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>userstamp</th>\n",
       "      <th>descr</th>\n",
       "      <th>current_release</th>\n",
       "      <th>full_descr</th>\n",
       "      <th>alive</th>\n",
       "      <th>for_release</th>\n",
       "      <th>display_name</th>\n",
       "      <th>project_id</th>\n",
       "      <th>avg_length</th>\n",
       "      <th>min_length</th>\n",
       "      <th>max_length</th>\n",
       "      <th>num_sequences</th>\n",
       "      <th>num_organisms</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>51</td>\n",
       "      <td>2022-09-22 15:14:59.500143</td>\n",
       "      <td>RNACEN</td>\n",
       "      <td>EXPRESSIONATLAS</td>\n",
       "      <td>636</td>\n",
       "      <td>Expression Atlas</td>\n",
       "      <td>Y</td>\n",
       "      <td>None</td>\n",
       "      <td>Expression Atlas</td>\n",
       "      <td>None</td>\n",
       "      <td>953.0</td>\n",
       "      <td>72.0</td>\n",
       "      <td>32709.0</td>\n",
       "      <td>11030</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5</td>\n",
       "      <td>2017-05-17 00:00:00.000000</td>\n",
       "      <td>RNACEN</td>\n",
       "      <td>VEGA</td>\n",
       "      <td>98</td>\n",
       "      <td>VEGA</td>\n",
       "      <td>N</td>\n",
       "      <td>None</td>\n",
       "      <td>VEGA</td>\n",
       "      <td>PRJEB4568</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>24</td>\n",
       "      <td>2017-05-02 00:00:00.000000</td>\n",
       "      <td>RNACEN</td>\n",
       "      <td>FLYBASE</td>\n",
       "      <td>614</td>\n",
       "      <td>FlyBase</td>\n",
       "      <td>Y</td>\n",
       "      <td>None</td>\n",
       "      <td>FlyBase</td>\n",
       "      <td>PRJ_FLY</td>\n",
       "      <td>765.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>21216.0</td>\n",
       "      <td>4210</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>50</td>\n",
       "      <td>2022-08-16 15:52:33.990145</td>\n",
       "      <td>RNACEN</td>\n",
       "      <td>PLNCDB</td>\n",
       "      <td>606</td>\n",
       "      <td>PLncDB</td>\n",
       "      <td>Y</td>\n",
       "      <td>None</td>\n",
       "      <td>PLncDB</td>\n",
       "      <td>None</td>\n",
       "      <td>6659.0</td>\n",
       "      <td>199.0</td>\n",
       "      <td>985945.0</td>\n",
       "      <td>936926</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id                  timestamp userstamp            descr  current_release  \\\n",
       "0  51 2022-09-22 15:14:59.500143    RNACEN  EXPRESSIONATLAS              636   \n",
       "1   5 2017-05-17 00:00:00.000000    RNACEN             VEGA               98   \n",
       "2  24 2017-05-02 00:00:00.000000    RNACEN          FLYBASE              614   \n",
       "3  50 2022-08-16 15:52:33.990145    RNACEN           PLNCDB              606   \n",
       "\n",
       "         full_descr alive for_release      display_name project_id  \\\n",
       "0  Expression Atlas     Y        None  Expression Atlas       None   \n",
       "1              VEGA     N        None              VEGA  PRJEB4568   \n",
       "2           FlyBase     Y        None           FlyBase    PRJ_FLY   \n",
       "3            PLncDB     Y        None            PLncDB       None   \n",
       "\n",
       "   avg_length  min_length  max_length  num_sequences  num_organisms  \n",
       "0       953.0        72.0     32709.0          11030              3  \n",
       "1         NaN         NaN         NaN              0              0  \n",
       "2       765.0        18.0     21216.0           4210              1  \n",
       "3      6659.0       199.0    985945.0         936926             80  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b31d46e9-7790-482d-b435-8c210c8316e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Close connection\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "977e0793-ebae-4d37-8ff1-5180f44b5a8c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_poi",
   "language": "python",
   "name": "env_poi"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
